//
//  XJLCreateGroupViewController.m
//  XJL_WeChat
//
//  Created by XJL on 16/8/23.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLCreateGroupViewController.h"

@interface XJLCreateGroupViewController ()

/** 所有的好友*/
@property(nonatomic,strong) NSArray *buddyList;
/** 选中的好友*/
@property(nonatomic,strong) NSMutableArray *selectedBuddyList;
@end

@implementation XJLCreateGroupViewController

-(NSMutableArray *)selectedBuddyList{
    if (_selectedBuddyList == nil) {
        _selectedBuddyList = [NSMutableArray array];
    }
    return _selectedBuddyList;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //显示所有好友
    self.buddyList = [[EaseMob sharedInstance].chatManager buddyList];
    // 2.显示多选功能
    /*
     * 1.设置tableView.editing属性 = YES;
     * 2.实现一个代理方法
     》-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
     return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
     }
     */
    
    self.tableView.editing = YES;
    
    //添加创建按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"创建" style:0 target:self action:@selector(createGroup)];
    
}


#pragma mark 创建组

-(void)createGroup{
    /*!
     @method
     @brief  创建群组（异步方法）。
     @param subject        群组名称
     @param description    群组描述
     @param invitees       默认群组成员（usernames，不需要包含创建者username）
     @param welcomeMessage 群组欢迎语
     @param styleSetting   群组属性配置
     @discussion
     函数执行完, 回调[group:didCreateWithError:]会被触发
     */
    EMGroupStyleSetting *setting = [[EMGroupStyleSetting alloc]init];
    setting.groupStyle = eGroupStyle_Default;
    XJLLog(@"%@",self.selectedBuddyList);
//    [EaseMob sharedInstance].chatManager asyncCreateGroupWithSubject:@"许佳莉群组" description:@"ios交流图" invitees:self.selectedBuddyList initialWelcomeMessage:@"欢迎加入本群" styleSetting:setting completion
    [[EaseMob sharedInstance].chatManager asyncCreateGroupWithSubject:@"许佳莉群组" description:@"ios交流图" invitees:self.selectedBuddyList initialWelcomeMessage:@"欢迎加入本群" styleSetting:setting completion:^(EMGroup *group, EMError *error) {
        
        if (!error) {
            XJLLog(@"创建群成功");
        }else{
            XJLLog(@"创建群失败");
        }
    } onQueue:nil];
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return self.buddyList.count;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.buddyList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupBuddy"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:0 reuseIdentifier:@"GroupBuddy"];
        
    }
    
    EMBuddy *buddy = self.buddyList[indexPath.row];
    cell.textLabel.text = buddy.username;
    
    return cell;
}

#pragma mark 选中

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取选中的用户名
    EMBuddy *buddy = self.buddyList[indexPath.row];
    NSString *username = buddy.username;
    
    [self.selectedBuddyList addObject:username];
}

#pragma mark 去选中

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取选中的用户名
    EMBuddy *buddy = self.buddyList[indexPath.row];
    NSString *username = buddy.username;
    
    [self.selectedBuddyList removeObject:username];
}

@end
